import unittest
from api.login import LoginAPI
import app


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()

    # def tearDown(self):

    # 登录成功
    def test01_case001(self):
        rep = self.login_api.login({"mobile": "13800000002", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(True, rep.json().get("success"))
        self.assertEqual(10000, rep.json().get("code"))
        self.assertIn("操作成功", rep.json().get("message"))
        app.token = "Bearer " + rep.json().get("data")
        print(app.token)


    # 不输入手机号
    def test01_case002(self):
        rep = self.login_api.login({"mobile": "", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 不输入密码
    def test01_case003(self):
        rep = self.login_api.login({"mobile": "13800000002", "password": ""})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 手机号长度小于11位
    def test01_case004(self):
        rep = self.login_api.login({"mobile": "1380000000", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 手机号长度大于11位
    def test01_case005(self):
        rep = self.login_api.login({"mobile": "138000000002", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 手机号输入非数字
    def test01_case006(self):
        rep = self.login_api.login({"mobile": "error", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 手机号未注册
    def test01_case007(self):
        rep = self.login_api.login({"mobile": "13800000113", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 多参
    def test01_case008(self):
        rep = self.login_api.login({"mobile": "13800000002", "password": "123456", "asdfa": "2314234"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(True, rep.json().get("success"))
        self.assertEqual(10000, rep.json().get("code"))
        self.assertIn("操作成功", rep.json().get("message"))

    # 少参-缺少手机号
    def test01_case009(self):
        rep = self.login_api.login({"password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 少参-密码
    def test01_case010(self):
        rep = self.login_api.login({"mobile": "13800000002"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 无参
    def test01_case011(self):
        rep = self.login_api.login(None)
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(99999, rep.json().get("code"))
        self.assertIn("抱歉，系统繁忙，请稍后重试", rep.json().get("message"))

    # 错误参数-手机号参数拼写错误
    def test01_case012(self):
        rep = self.login_api.login({"mobie": "13800000002", "password": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))

    # 错误参数-密码参数拼写错误
    def test01_case013(self):
        rep = self.login_api.login({"mobie": "13800000002", "paword": "123456"})
        print(rep.json())
        # 断言
        self.assertEqual(rep.status_code, 200)
        self.assertEqual(False, rep.json().get("success"))
        self.assertEqual(20001, rep.json().get("code"))
        self.assertIn("用户名或密码错误", rep.json().get("message"))
